<template>
	<view class="dmm-container dmm-container-grey">
		<view class="title">
			{{title}}
		</view>
		<text class="text" v-if="showPasswordTip">{{passwordTip}}</text>
		<view v-else class="dmm-space text"></view>
		
		<u-code-input key="originalPassword" v-if="current === 'originalPassword'" dot focus disabledDot space="14rpx" v-model="originalPassword" :maxlength="6" @finish="checkPassword"></u-code-input>
		<u-code-input key="password" class="password" v-if="current === 'password'" dot focus disabledDot space="14rpx" v-model="password" :maxlength="6" @finish="current = 'confirmPassword'"></u-code-input>
		<u-code-input key="confirmPassword" v-if="current === 'confirmPassword'" dot focus disabledDot space="14rpx" v-model="confirmPassword" :maxlength="6" @finish="updatePassword"></u-code-input>
		
		<u-modal :show="showModal" :title="modalTitle" :content='modalTip' @confirm="showModal = false" @close="showModal = false" closeOnClickOverlay></u-modal>
		<u-modal :show="showSuccessModal" confirmText="返回首页" closeOnClickOverlay @confirm="uniHistory.back(999)" @close="showSuccessModal = false">
			<view class="u-modal__content">
				<image src="../../static/imgs/icon_success.png" mode=""></image>
				<text class="text">支付密码设置成功</text>
			</view>
		</u-modal>
	</view>
</template>

<script>
	import {mapState, mapActions} from 'vuex'
	
	export default {
		data() {
			return {
				current: '',
				originalPassword: '',
				password: '',
				confirmPassword: '',
				showModal: false,
				modalTip: '',
				modalTitle: '提示',
				showSuccessModal: false,
			};
		},
		
		computed: {
			title: function() {
				const titleEnum = {
					originalPassword: '请先输入原始支付密码',
					password: !this.isFirstSetPassword ? '请输入新的支付密码' : '设置支付密码',
					confirmPassword: !this.isFirstSetPassword ? '请再次输入新的密码' : '请再次输入'
				}

				return titleEnum[this.current]
			},
			showPasswordTip: function() {
				if (['password', 'confirmPassword'].includes(this.current)) return true
			},
			passwordTip: function() {
				const passwordTipEnum = {
					password: '用于提现等身份验证',
					confirmPassword: '再次输入需一致，以确认密码'
				}
				
				return passwordTipEnum[this.current]
			}
		},
		
		methods: {
			...mapActions('setPassword', ['setPassword', 'checkOriginalPassword']),
			
			checkPassword(password) {
				this.current = 'password'
				console.log(this.password)
				// this.originalPassword = ''
				// this.checkOriginalPassword(password)
				// 	.then(() => {
				// 		this.current = 'password'
				// 	})
				// 	.catch(({code}) => {
				// 		if (code === 0) {
				// 			this.originalPassword = ''
				// 			this.modalTip = '原支付密码错误，请重新输入'
				// 			this.showModal = true
				// 		}
				// 	})
			},
			
			updatePassword(confirmPassword) {
				const {password} = this
				
				if (confirmPassword !== password) {
					this.modalTip = '两次输入的密码不一致，请重新输入'
					this.showModal = true
					this.confirmPassword = ''
					this.password = ''
					this.current = 'password'
				} else {
					this.setPassword({paymentPassword: this.password, operationType: this.isFirstSetPassword ? '1' : '2'})
						.then(() => this.showSuccessModal = true)
				}
			}
		},
		
		onLoad(options) {
			const {isFirstSetPassword} = options
			let title = ''
			
			if (isFirstSetPassword === 'true') {
				this.current = 'password'
				title = '设置密码'
			} else {
				this.current = 'originalPassword'
				this.isFirstSetPassword = true
				title = '修改密码'
			}
			
			uni.setNavigationBarTitle({
				title
			})
		}
	}
</script>

<style lang="scss">
	.title {
		text-align: center;
		padding-top: 160rpx;
		margin-bottom: 42rpx;
		font-size: 44rpx;
		font-weight: 700;
	}
	
	.text {
		display: block;
		text-align: center;
		font-size: 36rpx;
		height: 40rpx;
		line-height: 40rpx;
		margin-bottom: 194rpx;
	}
	
	.u-code-input {
		font-size: 36rpx;
		padding: 0 72rpx;
		
		/deep/ .u-code-input__item {
			height: 90rpx !important;
			width: 90rpx !important;
			border-radius: $uni-border-radius-lg;
			background-color: #fff;
			border: none !important;
		}
	}
	
	.u-popup {
		/deep/ .u-modal {
			width: 540rpx !important;
			
			.u-modal__content__text {
				color: $uni-text-color-muted;
				font-size: 28rpx;
				text-align: center;
			}
			.u-modal__title {
				color: $u-main-color;
			}
			
			.u-modal__button-group__wrapper__text {
				color: $u-main-color !important;
			}
		}
		
		.u-modal__content {
			text-align: center;
			image {
				height: 58rpx;
				width: 58rpx;
				margin-bottom: 17rpx;
			}
			.text {
				font-size: $uni-font-size-base;
				margin-bottom: 0rpx;
			}
		}
	}
</style>
